<?php

/**
 * Created by PhpStorm.
 * User1: Administrator
 * Date: 2017/3/23
 * Time: 15:00
 */

namespace App\Repositories\Manage;

use Illuminate\Support\Facades\DB;


class MenuRepo
{
    protected $menuTable = 'manage_menu';
    protected $menuModuleTable = 'manage_menu_module';
    protected $menuGroupTable = 'manage_menu_group';

    /**
     * 获取菜单列表
     */
    public function getMenuList()
    {
        return DB::table('manage_menu')
            ->join('manage_menu_group', 'manage_menu.group_id', '=', 'manage_menu_group.id')
            ->select('manage_menu.*', 'manage_menu_group.name as first_menu_name')
            ->orderBy('manage_menu.sort','desc')
            ->orderBy('manage_menu.id','asc')
            ->get();
    }


    /**
     * 获取单个菜单数据
     * @param $where    string
     * @param $whereArr  array
     */
    public function getMenu($where, $whereArr)
    {

        return DB::table($this->menuTable)->whereRaw($where, $whereArr)->first();
    }


    public function addMenu($name, $url, $sort,$firstMenuId)
    {

        $data = [
            'name' => $name,
            'url' => $url,
            'sort' => $sort,
            'group_id'=>$firstMenuId
        ];
        return DB::table($this->menuTable)->insertGetId($data);
    }


    public function updateMenu($id, $name, $url, $sort,$firstMenuId)
    {

        $data = [
            'name' => $name,
            'url' => $url,
            'sort' => $sort,
            'group_id'=>$firstMenuId
        ];
        return DB::table($this->menuTable)->where('id', $id)->update($data);
    }

    public function delMenu($id)
    {

        return DB::table($this->menuTable)->where('id', $id)->delete();
    }

    /**
     * 获取模块列表
     * @param $where    string
     * @param $whereArr  array
     */
    public function getModuleList($where, $whereArr)
    {
        return DB::table($this->menuModuleTable)->whereRaw($where, $whereArr)->get();
    }

    /**
     * 获取单个模块数据
     * @param $where    string
     * @param $whereArr  array
     */
    public function getModule($where, $whereArr)
    {

        return DB::table($this->menuModuleTable)->whereRaw($where, $whereArr)->first();
    }

    public function addMenuModule($menuId, $moduleName, $value, $action)
    {

        $data = [
            'menu_id' => $menuId,
            'module_name' => $moduleName,
            'value' => $value,
            'action' => $action
        ];
        return DB::table($this->menuModuleTable)->insertGetId($data);
    }


    public function editMenuModule($id, $moduleName, $value, $action)
    {

        $data = [
            'module_name' => $moduleName,
            'value' => $value,
            'action' => $action
        ];
        return DB::table($this->menuModuleTable)->where('id', $id)->update($data);
    }


    /**
     * 获取菜单对应的模块列表
     */
    public function getMenuModule()
    {
        $menuData = DB::table($this->menuTable)->orderBy('sort', 'desc')->select('id', 'name')->get();
        foreach ($menuData as $k => $val) {
            $data = $this->getModuleList('menu_id = ?', [$val->id]);
            $menuData[$k]->menuModule = $data;
        }
        return $menuData;

    }

    /**
     * 获取用户菜单
     * @param $arr  array
     */
    public function getUserMenu($arr)
    {
        $data = DB::table($this->menuTable)->whereIn('id', $arr)->get();
        return $data;
    }

    /**一级菜单列表
     * @return mixed
     */
    public function getFirstMenuList()
    {
        $data = DB::table($this->menuGroupTable)->orderBy('sort','desc')->get();
        return $data;
    }

    public function getFirstMenu($id)
    {
        $data = DB::table($this->menuGroupTable)->where('id', $id)->first();
        return $data;
    }

    public function addFirstMenu($data)
    {
        $data = DB::table($this->menuGroupTable)->insertGetId($data);
        return $data;
    }

    public function editFirstMenu($id, $data)
    {
        return DB::table($this->menuGroupTable)->where('id', $id)->update($data);

    }

}